package com.hongxun.project.assets.pDevice.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxun.project.assets.pDevice.mapper.PDeviceMapper;
import com.hongxun.project.assets.pDevice.domain.PDevice;
import com.hongxun.project.assets.pDevice.service.IPDeviceService;
import com.hongxun.common.utils.text.Convert;

/**
 * 配电设备采集 服务层实现
 *
 * @author hongxun
 * @date 2019-03-19
 */
@Service
public class PDeviceServiceImpl implements IPDeviceService {
    @Autowired
    private PDeviceMapper pDeviceMapper;

    /**
     * 查询配电设备采集信息
     *
     * @param id 配电设备采集ID
     * @return 配电设备采集信息
     */
    @Override
    public PDevice selectPDeviceById(String id, String taskId) {
        return pDeviceMapper.selectPDeviceById(id, taskId);
    }

    @Override
    public PDevice selectPDeviceByIdForState(String id, String taskId) {
        return pDeviceMapper.selectPDeviceByIdForState(id, taskId);
    }

    /**
     * 查询配电设备采集列表
     *
     * @param pDevice 配电设备采集信息
     * @return 配电设备采集集合
     */
    @Override
    public List<PDevice> selectPDeviceList(PDevice pDevice) {
        return pDeviceMapper.selectPDeviceList(pDevice);
    }

    /**
     * 新增配电设备采集
     *
     * @param pDevice 配电设备采集信息
     * @return 结果
     */
    @Override
    public int insertPDevice(PDevice pDevice) {
        pDevice.setId(UUID.randomUUID().toString());
        return pDeviceMapper.insertPDevice(pDevice);
    }

    /**
     * 修改配电设备采集
     *
     * @param pDevice 配电设备采集信息
     * @return 结果
     */
    @Override
    public int updatePDevice(PDevice pDevice) {
        return pDeviceMapper.updatePDevice(pDevice);
    }

    /**
     * 删除配电设备采集对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePDeviceByIds(String ids) {
        return pDeviceMapper.deletePDeviceByIds(Convert.toStrArray(ids));
    }

    @Override
    public int deletePDeviceByIdsAndTaskIds(String ids, String taskId) {
        return pDeviceMapper.deletePDeviceByIdsAndTaskIds(Convert.toStrArray(ids), Convert.toStrArray(taskId));
    }


    @Override
    public void insertOrReplaceOrDelete(List<PDevice> entities) {
        for (PDevice d : entities) {
//			if (d.getDatastate().equals("删除")) {
////				pDeviceMapper.deletePDeviceByIds(Convert.toStrArray(d.getId()));
//				pDeviceMapper.deletePDeviceByIdsAndTaskIds(Convert.toStrArray(d.getId()) , Convert.toStrArray(d.getTaskId()));
//			} else {
            PDevice pDevice = pDeviceMapper.selectPDeviceById(d.getId(), d.getTaskId());
            if (pDevice == null) {
                pDeviceMapper.insertPDevice(d);
            } else {
                pDeviceMapper.updatePDevice(d);
//				}
            }
        }
    }

}
